package com.droidexchange.adapter;

import android.app.Activity;
import android.os.Debug;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import com.droidexchange.R;
import com.droidexchange.custom.AnswerTabPanel;
import com.droidexchange.main.DeMajorPost;

public class AnswersAdapter extends ArrayAdapter<DeMajorPost> implements OnScrollListener
{
	Activity				activity;
	private AnswerTabPanel	floatingPanel	= null;

	public AnswersAdapter(Activity activity, int resourceId)
	{
		super(activity, resourceId);
		this.activity = activity;
	}

	public void setFloatingPanel(AnswerTabPanel answerTabPanel)
	{
		this.floatingPanel = answerTabPanel;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View elem;
		if (convertView == null)
			elem = LayoutInflater.from(activity).inflate(R.layout.answer_tab, parent, false);
		else
			elem = convertView;

		DeMajorPost post = getItem(position);

		AnswerTabPanel answerTabPanel = (AnswerTabPanel) elem.findViewById(R.id.atabpanel_holder);
		// answerTabPanel.setOnClickListener(post);
		answerTabPanel.setViewContents(post);

		post.attachDisplayText(elem);

		elem.setTag(Integer.toString(post.majorPost.getPostId()));
		return elem;
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
	{
		try
		{
			if (floatingPanel == null)
				return;

			int position = view.pointToPosition(0, 1);

			floatingPanel.setVisibility(View.GONE);
			if (position > 0 && position < getCount())
			{
				floatingPanel.setViewContents(getItem(position - 1));
				floatingPanel.setVisibility(View.VISIBLE);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState)
	{
	}

}
